﻿using RsCms.Core.User.Aggregate.Entities;
using RsCode;
using RsCode.Domain;
using Rswl.Common.Core.User.Aggregate.Entities;
using Rswl.Common.Core.User.Aggregate.ValueObject;

namespace Rswl.Common.Core.User
{
    public class UserRepository:Repository<UserInfo>, IUserRepository
    {
        public UserRepository(IApplicationDbContext applicationDbContext,
		 IRepository<UserRoleInfo> userRoleRepository,
		IRepository<ShippingAddressInfo> addressRepository,
            IRepository<UserExtInfo> userExtRepository,
            IRepository<ShareInfo> shareRepository,
            IRepository<UserGradeInfo> usergradeRepository):base(applicationDbContext)
        {
            this.userExtRepository = userExtRepository;
            this.shareRepository = shareRepository;
            this.addressRepository= addressRepository;
            this.userGradeRepository = usergradeRepository;
			this.userRoleRepository = userRoleRepository;
		}

        IRepository<UserRoleInfo> userRoleRepository;
			   IRepository<UserExtInfo> userExtRepository { get; set; }
        IRepository<UserGradeInfo> userGradeRepository;
      
         IRepository<ShareInfo> shareRepository { get; set; }
        IRepository<ShippingAddressInfo> addressRepository { get; set; }
        
        
        public IRepository<UserExtInfo> UserExtRepository => userExtRepository;
        public IRepository<ShareInfo> UserShareRepository => shareRepository;
        public IRepository<ShippingAddressInfo> UserShipAddressRepository => addressRepository;
        public IRepository<UserGradeInfo> UserGradeRepository => userGradeRepository;

		 

		public async Task<UserInfo> GetUserInfoAsync(string userId)
        {
            return await Select.Where(x => x.UserId == userId).IncludeMany(x => x.UserExtInfos).ToOneAsync();
        }
        
        
        

        public async Task<bool> ExistUserAsync()
        {
            var user = await Select.FirstAsync();
            return user!=null;
        }
         public async Task<List<UserInfo>>GetAdminAsync()
        {
            var list = await  Select.Where(x => x.Role == UserRole.Admin).ToListAsync();
            return list;
        }

        public async Task<PageData<UserInfo>>GetPageAsync(int page,int rows)
        {
            return await PageAsync(page, rows);
        }

        public async Task<PageData<UserInfo>> PageFansAsync(int page, int rows,string userId)
        {
            var select= Select.Page(page, rows).Where(x=>x.InviteUserId==userId).OrderBy(x=>x.RegisterDate);
            return await PageAsync(page,rows,select);
        }



        public async Task SaveAsync(UserInfo info)
        {
            var user = await GetUserInfoAsync(info.UserId);
            if(user==null)
            {
                info.InviteCode = info.CreateInviteCode();
                Insert(info);
            }else
            {
                Update(info);
            }
        }

       
        public async Task SaveAsync(ShareInfo info)
        {
            shareRepository.InsertOrUpdate(info);
        }
       
        public async Task<ShareInfo> GetShareInfoAsync(string userId)
        {
            var shareInfo = await shareRepository.Select.Where(x => x.UserId == userId).FirstAsync();
            if(shareInfo==null)
            {
                shareInfo = new ShareInfo(userId, "");
                shareRepository.Insert(shareInfo);
            }
            return shareInfo;
        }
        
      
        public async Task<List<UserExtInfo>>GetExtInfosAsync(string userId)
        {
            return await userExtRepository.Select.Where(x=>x.UserId== userId).ToListAsync();
        }
        

        public async Task<List<ShippingAddressInfo>> GetShippingAddressAsync(string userId)
        {
           return await addressRepository.Select.Where(x=>x.UserId==userId).ToListAsync();
        }

        
        
    }
}
